package com.itheima.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.QuestionBelongTo;
import com.itheima.domain.db.Questionnaire;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.domain.db.SoulQuestionOption;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.QuestionAdminVo;
import com.itheima.domain.vo.QuestionVo;
import com.itheima.domain.vo.QuestionnaireAndQuestionVo;
import com.itheima.service.db.QuestionBelongToService;
import com.itheima.service.db.QuestionnaireService;
import com.itheima.service.db.SoulQuestionOptionService;
import com.itheima.service.db.SoulQuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class QuestionManger {

    //问题表
    @Reference
    private SoulQuestionService soulQuestionService;


    //问题选项
    @Reference
    private SoulQuestionOptionService soulQuestionOptionService;

    //问卷选项
    @Reference
    private QuestionnaireService questionnaireService;

    //问题归属表
    @Reference
    private QuestionBelongToService questionBelongToService;



    /*
     *  分页查询灵魂测试表
     *
     * @param num   分页数据
     * @param size  分页数据`
     * @author
     * @date
     * @return com.itheima.domain.vo.PageBeanVo
     */

    public PageBeanVo SoulQuestionList(Integer pageNum, Integer PageSize) {

        //分页数据
        PageBeanVo pageBeanVo = soulQuestionService.SoulQuestionByPage(pageNum, PageSize);

        //分页后的问题集合
        List<SoulQuestion> items = (List<SoulQuestion>) pageBeanVo.getItems();

        //Vo list
        List<QuestionVo> voList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(items)){
            //测灵魂问题
            for (SoulQuestion question : items) {
                //题目选项合并的Vo
                QuestionVo questionVo = new QuestionVo();
                //赋值题目
                questionVo.setQuestion(question.getStem());

                //查询每道题的选项
                List<SoulQuestionOption> optionList =
                        soulQuestionOptionService.findByQuestionId(question.getId());

                //Vo选项赋值
                if (optionList.size()>0){
                    questionVo.setOptionA(optionList.get(0).getContent());
                }
                if (optionList.size()>1){
                    questionVo.setOptionB(optionList.get(1).getContent());
                }
                if (optionList.size()>2){
                    questionVo.setOptionC(optionList.get(2).getContent());
                }
                if (optionList.size()>3){
                    questionVo.setOptionD(optionList.get(3).getContent());
                }
                if (optionList.size()>4){
                    questionVo.setOptionE(optionList.get(4).getContent());
                }
                if (optionList.size()>5){
                    questionVo.setOptionF(optionList.get(5).getContent());
                }
                if (optionList.size()>6){
                    questionVo.setOptionG(optionList.get(6).getContent());
                }

                voList.add(questionVo);

            }
        }

        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }


    /*
     *  问题列表
     *
     * @param num   当前页
     * @param pageSize  每页显示条数
     * @author
     * @date
     * @return com.itheima.domain.vo.PageBeanVo
     */
    //查询问题信息
    public PageBeanVo findQuestions(int currentPage, int pageSize) {

        PageBeanVo pageBeanVo = soulQuestionService.SoulQuestionByPage(currentPage, pageSize);

        List<SoulQuestion> items = (List<SoulQuestion>) pageBeanVo.getItems();

        ArrayList<QuestionAdminVo> voList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(items)){

            for (SoulQuestion soulQuestion : items) {
                Questionnaire questionnaire=questionnaireService.findQuestionnaireByQuestionId(soulQuestion.getQuestionnaireId().intValue());

                QuestionAdminVo questionAdminVo = new QuestionAdminVo();
                //问题Id
                questionAdminVo.setId(soulQuestion.getId().intValue());
                //问题所属问卷
                questionAdminVo.setQuestionnaire(questionnaire.getName());
                //问题详情
                questionAdminVo.setQuestion(soulQuestion.getStem());

                voList.add(questionAdminVo);

            }
        }
        pageBeanVo.setItems(voList);


        return pageBeanVo;

    }


    /*
     *
     *
     * @param soulQuestion SoulQuestion实体类数据
     * @author
     * @date
     * @return com.itheima.domain.db.SoulQuestion
     */

    public SoulQuestion showEditQuestion(SoulQuestion soulQuestion) {
        return soulQuestionService.findByQuestionId(soulQuestion.getId());
    }

    /*
     *
     *
     * @param soulQuestion SoulQuestion实体类数据
     * @author
     * @date
     * @return java.lang.String
     */

    public String saveQuestions(SoulQuestion soulQuestion) {
        String msg="";
       if(soulQuestionService.saveQuestions(soulQuestion)>0){
           msg="ok";
       }
        return msg;
    }

    /*
     *问题修改
     * @param soulQuestion
     * @author
     * @date
     * @return java.lang.String 成功返回Ok响应
     */

    public String updateSoulQuestions(SoulQuestion soulQuestion) {
        String msg="";
        if(soulQuestionService.updateSoulQuestions(soulQuestion)>0){
            msg="ok";
        }
        return msg;
    }

    /*
     *
     *
     * @param soulQuestion SoulQuestion实体类数据
     * @author
     * @date
     * @return java.lang.String 成功返回Ok响应
     */

    public String deleteQuestions(SoulQuestion soulQuestion) {
        String msg="";
        if(soulQuestionService.deleteQuestions(soulQuestion)>0){
            msg="ok";
        }
        return msg;
    }
}
